home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / freemem.c < prev    next >
Encoding:
Text File  |  1992-09-14  |  4.7 KB  |  179 lines  |  [TEXT/R*ch]

  1.  
  2. long    App_partitions_total( void );
  3.  
  4. extern    WindowPtr    g_freemem_window;
  5. extern    Boolean        g_use_sysheap;
  6.  
  7. typedef enum {
  8.     sm_unknown,
  9.     sm_no_temp_mem,
  10.     sm_temp_mem,
  11.     sm_temp_mem_is_real
  12. } sm_temp_mem_status;
  13.  
  14. extern sm_temp_mem_status    MF_mem_status;
  15.  
  16. long        g_last_freemem = 0L;
  17. long        g_last_freesys = 0L;
  18. int            g_bar_length;
  19. long        g_allmem;                /* application memory */
  20. long        g_sysheap_size;
  21.  
  22. #define BAR_HEIGHT    10
  23. #define SIDE_MARGIN    5
  24. #define BAR_TOP        20
  25. #define SBAR_TOP    40
  26. #define SYSHEAP_MAX    50000L
  27. #define SYSHEAP_THRESHOLD    30000L
  28. #define BASELINESKIP    15
  29.  
  30. void    Update_memory_indicators( void );
  31. void    Update_freemem( void );
  32. pascal long FreeMemSys( void );
  33.  
  34. void
  35. Update_freemem()
  36. {
  37.     GrafPtr    save_port;
  38.     Str255    mem_str;
  39.     Rect    myrect;
  40.  
  41.     if ( ((WindowPeek)g_freemem_window)->visible )
  42.     {
  43.         GetPort( &save_port );
  44.     
  45.         SetPort( g_freemem_window );
  46.         if (g_use_sysheap)
  47.             g_bar_length = (g_freemem_window->portRect.right -
  48.                 g_freemem_window->portRect.left - 4*SIDE_MARGIN) / 2;
  49.         else
  50.             g_bar_length = g_freemem_window->portRect.right -
  51.                 g_freemem_window->portRect.left - 2*SIDE_MARGIN;
  52.         EraseRgn( g_freemem_window->visRgn );
  53.         MoveTo( SIDE_MARGIN, BASELINESKIP );
  54.         Move( StringWidth("\p9999999"), 0 );
  55.         DrawString( "\p of " );
  56.         g_allmem = StripAddress( (Ptr)CurStackBase ) -
  57.             StripAddress( (Ptr)ApplZone );
  58.         NumToString( g_allmem, mem_str );
  59.         DrawString( mem_str );
  60.         MoveTo( SIDE_MARGIN, 3*BASELINESKIP );
  61.         DrawString("\pApplication memory");
  62.  
  63.         SetRect( &myrect, SIDE_MARGIN, 2*BASELINESKIP - BAR_HEIGHT,
  64.             SIDE_MARGIN + g_bar_length, 2*BASELINESKIP );
  65.         InsetRect( &myrect, -1, -1 );
  66.         FrameRect( &myrect );
  67.  
  68.         if (g_use_sysheap)
  69.         {
  70.             MoveTo( 3*SIDE_MARGIN + g_bar_length, BASELINESKIP );
  71.             Move( StringWidth("\p9999999"), 0 );
  72.             DrawString( "\p of " );
  73.             g_sysheap_size = StripAddress( MFTopMem() ) -
  74.                 StripAddress((Ptr)SysZone->bkLim) - App_partitions_total();
  75.             NumToString( g_sysheap_size, mem_str );
  76.             DrawString( mem_str );
  77.             MoveTo( 3*SIDE_MARGIN + g_bar_length, 3*BASELINESKIP );
  78.             DrawString("\pTemporary Memory");
  79.             OffsetRect (&myrect, g_bar_length + 2*SIDE_MARGIN, 0 );
  80.             FrameRect( &myrect );
  81.         }
  82.         
  83.         g_last_freemem = 0; /* only temporary, to force an update */
  84.         Update_memory_indicators();
  85.  
  86.         SetPort( save_port );
  87.     }
  88. }
  89.  
  90. /* --------------------- Update_memory_indicators -------------------- */
  91. /* 
  92.     This routine is invoked when the memory indicators need to be updated.
  93.     It does this with a minimal amount of drawing, and does NOT suffice
  94.     as a response to an update event.
  95. */
  96. void
  97. Update_memory_indicators()
  98. {
  99.     
  100.     GrafPtr    save_port;
  101.     register long        freemem, freesys;
  102.     long    purgemem, contig;
  103.     Str255    mem_str;
  104.     Rect    myrect;
  105.  
  106.     if ( ((WindowPeek)g_freemem_window)->visible )
  107.     {
  108.         PurgeSpace( &purgemem, &contig );
  109.         freemem = FreeMem();
  110.         if (g_use_sysheap)
  111.             freesys = MFFreeMem();
  112.         else
  113.             freesys = 0L;
  114.         if ( (freemem != g_last_freemem) || 
  115.             (g_use_sysheap && (freesys != g_last_freesys)) )
  116.         {
  117.             GetPort( &save_port );
  118.         
  119.             SetPort( g_freemem_window );
  120.     
  121.             /* Redraw numerical indication of free memory */
  122.             SetRect( &myrect, SIDE_MARGIN, 0,
  123.                 SIDE_MARGIN + StringWidth("\p9999999"),
  124.                 2*BASELINESKIP - BAR_HEIGHT - 1 );
  125.             EraseRect( &myrect );
  126.             MoveTo( SIDE_MARGIN, BASELINESKIP );
  127.             NumToString( freemem, mem_str );
  128.             DrawString( mem_str );
  129.             
  130.             /* Adjust free memory bar */
  131.             myrect.bottom = 2*BASELINESKIP;
  132.             myrect.top = myrect.bottom - BAR_HEIGHT;
  133.             myrect.right = myrect.left +
  134.                 ( (float)freemem / (float)g_allmem ) * g_bar_length;
  135.             FillRect( &myrect, dkGray );
  136.             myrect.left = myrect.right;
  137.             myrect.right = myrect.left +
  138.                 ( (float)(purgemem - freemem) / (float)g_allmem )
  139.                     * g_bar_length;
  140.             FillRect( &myrect, ltGray );
  141.             myrect.left = myrect.right;
  142.             myrect.right = SIDE_MARGIN + g_bar_length;
  143.             EraseRect( &myrect );
  144.             
  145.             if (g_use_sysheap)
  146.             {
  147.                 myrect.left = 3*SIDE_MARGIN + g_bar_length;
  148.                 if (freesys > g_sysheap_size)
  149.                     freesys = g_sysheap_size;
  150.                 myrect.right = myrect.left +
  151.                     (float)freesys / (float)g_sysheap_size * g_bar_length;
  152.                 FillRect( &myrect, dkGray );
  153.                 myrect.left = myrect.right;
  154.                 myrect.right = 3*SIDE_MARGIN + 2*g_bar_length;
  155.                 EraseRect( &myrect );
  156.                 
  157.                 SetRect( &myrect, 3*SIDE_MARGIN + g_bar_length, 0,
  158.                     3*SIDE_MARGIN + g_bar_length +
  159.                     StringWidth("\p9999999 of 9999999"),
  160.                     2*BASELINESKIP - BAR_HEIGHT - 1 );
  161.                 EraseRect( &myrect );
  162.                 MoveTo( 3*SIDE_MARGIN + g_bar_length, BASELINESKIP );
  163.                 NumToString( freesys, mem_str );
  164.                 DrawString( mem_str );
  165.                 MoveTo( 3*SIDE_MARGIN + g_bar_length +
  166.                     StringWidth("\p9999999"),
  167.                     BASELINESKIP );
  168.                 DrawString("\p of ");
  169.                 NumToString( g_sysheap_size, mem_str );
  170.                 DrawString( mem_str );
  171.             }
  172.  
  173.             SetPort( save_port );
  174.             g_last_freemem = freemem;
  175.             g_last_freesys = freesys;
  176.         }
  177.     }
  178. }
  179.